#include <queue>
#include <vector>
using namespace std;

vector<int> ans;
void topo(vector<vector<int>>& graph) {
    int n = graph.size();
    vector<int> indegree(n, 0);
    for (int i = 0; i < n; i++) {
        for (int nei : graph[i]) indegree[nei]++;
    }
    queue<int> Q;
    for (int i = 0; i < n; i++) {
        if (indegree[i] == 0) { Q.push(i); }
    }
    while (!Q.empty()) {
        int fr = Q.front();
        Q.pop();
        ans.emplace_back(fr);
        for (int nei : graph[fr]) {
            indegree[nei]--;
            if (indegree[nei] == 0) { Q.push(nei); }
        }
    }
}

vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
    vector<vector<int>> graph(numCourses, vector<int>());
    for (int i = 0; i < prerequisites.size(); i++) {
        graph[prerequisites[i][1]].emplace_back(prerequisites[i][0]);
    }
    topo(graph);
    if (ans.size() == numCourses) return ans;
    return {};
}